//
//  MineViewController.m
//  WuJieLive
//
//  Created by iMac on 2017/10/20.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "MineOrderTableViewCell.h"
#import "TicketController.h"
#import "GlodLibraryController.h"
#import "PopupView.h"
#import "MessageViewController.h"
#import "MyOrderViewController.h"
#import "MyWalletController.h"
#import "HelpController.h"
#import "ShoppingAddressController.h"
#import "MyCollectionController.h"
#import "SettingViewController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource,CellBtnDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *userImage;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UIButton *QRcodeBtn;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (nonatomic , strong) NSString * string;

@property (nonatomic , strong) UITableView * tableview;

@property (nonatomic , strong) PopupView * popView;

@property (nonatomic , strong) NSArray * imageArray;
@property (nonatomic , strong) NSArray * titleArray;
@property (nonatomic , strong) NSArray * rowImageArray;
@property (nonatomic , strong) NSArray * rowTieleArray;

@property (nonatomic , copy) NSString * waitPayForString;
@property (nonatomic , copy) NSString * waitSendGoodsString;
@property (nonatomic , copy) NSString * waitGetGoodsString;
@property (nonatomic , copy) NSString * waitCommentString;
@property (nonatomic , copy) NSString * afterBuyString;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
    if (self.isLogin) {
        NSDictionary * dict = @{@"token":self.userToken};
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@/member/index",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            if (isSuccess) {
                // 订单状态 1待付款 6待发货 11待收货 16待评论 21关闭 30完成
                self.waitPayForString = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"order_unread"][@"1"]];
                self.waitCommentString = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"order_unread"][@"16"]];
                self.waitGetGoodsString = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"order_unread"][@"11"]];
                self.waitSendGoodsString = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"order_unread"][@"6"]];
                self.afterBuyString = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"sale_unread"]];
                
                self.QRcodeBtn.hidden = NO;
                self.nickName = self.userName.text = responseObject[@"data"][@"user_name"];
                [self.userImage sd_setImageWithURL:[NSURL URLWithString:responseObject[@"data"][@"portrait"]]];
                self.servicePhoneNum = responseObject[@"data"][@"customer_service_phone_show"];
                self.userImageString = responseObject[@"data"][@"portrait"];
                self.QRcodeUrl = responseObject[@"data"][@"qrcode_url"];
                self.accountMoney = [responseObject[@"data"][@"account_money"] length] > 0 ? responseObject[@"data"][@"account_money"] : @"";
                [self.tableview reloadData];
            }
        } failure:^(NSError *error) {
            
        }];
    }else
    {
        self.QRcodeBtn.hidden = YES;
        self.userImage.image = Image(@"icon_默认头像_90");
        self.userName.text = @"点击登录";
        self.waitPayForString = @"0";
        self.waitCommentString = @"0";
        self.waitGetGoodsString = @"0";
        self.waitSendGoodsString = @"0";
        self.afterBuyString = @"0";
        [self.tableview reloadData];
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
}

- (void)setUI
{
    self.imageArray = @[@"icon_我的钱包",@"icon_我的收藏",@"icon_收货地址",@"icon_邀请好友"];
    self.titleArray = @[@"我的钱包",@"我的收藏",@"收货地址",@"邀请好友"];
    
    self.rowImageArray = @[@"icon_客服热线",@"icon_帮助"];
    self.rowTieleArray = @[@"客服热线",@"帮助反馈"];
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,225, kScreenWidth, kScreenHeight - 225 - 44) style:(UITableViewStylePlain)];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = Color(245, 245, 245);
    [self.tableview registerClass:[MineOrderTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [self.tableview registerClass:[MineTableViewCell class] forCellReuseIdentifier:@"cell2"];
    [self.view addSubview:self.tableview];
    self.tableview.tableFooterView = [[UIView alloc]init];
    
    self.userImage.layer.masksToBounds = YES;
    self.userImage.layer.cornerRadius = 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]init];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if(section == 1)
    {
        return 4;
    }else
    {
        return 2;
    }
}

- (void)orderBtnAction:(NSInteger)tag
{
    MyOrderViewController * vc = [[MyOrderViewController alloc]init];
    if (tag == 1000) {
        vc.indexString = @"1";
    }else if (tag == 1001){
        vc.indexString = @"2";
    }else if (tag == 1002){
        vc.indexString = @"3";
    }else if (tag == 1003){
        vc.indexString = @"4";
    }else if (tag == 1004){
//        vc.indexString = @"";
    }
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MineOrderTableViewCell * orderCell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        orderCell.delegate = self;
        if ([self.waitPayForString integerValue] > 0) {
            [orderCell setbadge:self.waitPayForString withTag:1000];
        }
        if ([self.waitSendGoodsString integerValue] > 0){
            [orderCell setbadge:self.waitSendGoodsString withTag:1001];
        }
        if ([self.waitGetGoodsString integerValue] > 0){
            [orderCell setbadge:self.waitSendGoodsString withTag:1002];
        }
        if ([self.waitCommentString integerValue] > 0){
            [orderCell setbadge:self.waitCommentString withTag:1003];
        }
        if ([self.afterBuyString integerValue] > 0){
            [orderCell setbadge:self.afterBuyString withTag:1004];
        }
        orderCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return orderCell;
    }else if (indexPath.section == 1) {
        MineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell.leftImage.image = Image(self.imageArray[indexPath.row]);
        cell.label.text = self.titleArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cell.rightLabel.hidden = NO;
            cell.rightLabel.text = self.accountMoney ? [NSString stringWithFormat:@"余额：%@",self.accountMoney] : @"";
        }
        return cell;
    }
    else if(indexPath.section == 2){
        MineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell.leftImage.image = Image(self.rowImageArray[indexPath.row]);
        cell.label.text = self.rowTieleArray[indexPath.row];
        cell.rightLabel.hidden = YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cell.rightLabel.hidden = NO;
            cell.rightLabel.text = self.servicePhoneNum;
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 120;
    }else
    {
        return 50;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.isLogin) {
        if (indexPath.section == 0) {
            
            [self toMyAllOrder];
            
        }else if(indexPath.section == 1){
            switch (indexPath.row) {
                case 0:
                {
                    [self toMyWallet];
                }
                    break;
                case 1:
                {
                    [self toMyCollect];
                }
                    break;
                case 2:
                {
                    [self toAddress];
                }
                    break;
                case 3:
                {
                    [self inviteFriend];
                }
                    break;
                    
                default:
                    break;
            }
            
        }else if (indexPath.section == 2){
            if (indexPath.row == 0) {
                
                [self callPhone];
                
            }else if(indexPath.row == 1){
                
                [self feedback];
            }
            
        }
    }else
    {
        BaseNavigationController * navi = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

#pragma mark 帮助反馈
- (void)feedback
{
    HelpController * vc = [[HelpController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 打客服热线
- (void)callPhone
{
//    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"客服热线" message:@"400-115-6966" preferredStyle:(UIAlertControllerStyleAlert)];
//    UIAlertAction * done = [UIAlertAction actionWithTitle:@"呼叫" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
//
        NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.servicePhoneNum];
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        
//    }];
//    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//    [alert addAction:done];
//    [alert addAction:cancel];
//    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark 邀请好友
- (void)inviteFriend
{
    
}

#pragma mark 到收货地址
- (void)toAddress
{
    ShoppingAddressController * vc = [[ShoppingAddressController alloc]initWithNibName:@"ShoppingAddressController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 到我的收藏
- (void)toMyCollect
{
    MyCollectionController * vc = [[MyCollectionController alloc]initWithNibName:@"MyCollectionController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 到我的钱包
- (void)toMyWallet
{
    MyWalletController * vc = [[MyWalletController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 到我的全部订单界面
- (void)toMyAllOrder
{
    if (self.isLogin) {
        MyOrderViewController * vc = [[MyOrderViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else
    {
        BaseNavigationController * navi = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

#pragma mark 消息
- (IBAction)messageAction:(id)sender {
    if (self.isLogin) {
        MessageViewController * vc = [[MessageViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else
    {
        BaseNavigationController * navi = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

#pragma mark 设置
- (IBAction)settingAction:(id)sender {
    
    SettingViewController * vc = [[SettingViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark 二维码按钮点击
- (IBAction)QRcodeBtnAction:(id)sender {
    self.zh_popupController = [zhPopupController new];
    [self.zh_popupController presentContentView:self.popView duration:0.2 springAnimated:NO];
}


#pragma mark 用户信息
- (IBAction)userInfoBtn:(id)sender {
    if (self.isLogin) {
        
    }else
    {
        BaseNavigationController * navi = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

#pragma mark 卡券包
- (IBAction)tickerAction:(id)sender {
    if (self.isLogin) {
        TicketController * vc = [[TicketController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else
    {
        BaseNavigationController * navi = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

//- (BOOL)userIsLogin
//{
//    return self.isLogin;
//}

#pragma mark 小金库
- (IBAction)goldLibrary:(id)sender {
    if (self.isLogin) {
        GlodLibraryController * vc = [[GlodLibraryController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else
    {
        BaseNavigationController * navi = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (PopupView *)popView
{
    if (!_popView) {
        _popView = [[PopupView alloc]initWithFrame:CGRectMake( 40,0, kScreenWidth - 80, 450)];
        [_popView.headerImage sd_setImageWithURL:[NSURL URLWithString:self.userImageString]];
        [_popView.QRcodeImage sd_setImageWithURL:[NSURL URLWithString:self.QRcodeUrl]];
        _popView.textLabel.text = [NSString stringWithFormat:@"%@ 邀请您\n一起加入无界生活循环消费系统",self.nickName];
    }
    return _popView;
}


@end
